package com.suxin.socket.yinfei.handler.chain;

import com.suxin.socket.yinfei.handler.AbstractMessageHandler;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.Optional;

/**
 * @author Tang
 * @classname AbstractChainMessageHandler
 * @description [  ]
 * @date 2021/8/19 22:46
 */
@Slf4j
public abstract class AbstractChainMessageHandler extends AbstractMessageHandler<Object, Object> {

    @Getter
    private AbstractChainMessageHandler chainMessageHandler;

    public AbstractChainMessageHandler(AbstractChainMessageHandler chainMessageHandler) {
        this.chainMessageHandler = chainMessageHandler;
    }

    @Override
    public Object doHandler(Object msg) {
        final Object[] result = {doChainHandler(msg)};
        Optional.ofNullable(getChainMessageHandler()).ifPresent(chain -> result[0] = chain.doHandler(result[0]));
        return result[0];
    }

    /**
     * doHandler
     * @param msg
     * @return
     */
    protected abstract Object doChainHandler(Object msg);
}